#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxm=200000;

ll n, k, d, p, s, a[maxm+1];
int m;

ll calc(int i, ll b) {
    return (m-i+k)*min(b, n)+min((d-n*p-s*b)/a[i], n)+n*(i-1);
}

ll getMin(ll a, ll b, ll c, ll d) {return(a-c*d-c+b)/b;}

int main() {
    freopen("array.in", "r", stdin);
    freopen("array.out", "w", stdout);

    int t;
    scanf("%d", &t);
    for (int ti=1; ti<=t; ti++) {
        scanf("%lld %d %lld %lld", &n, &m, &k, &d);
        p=s=0;
        for (int i=1; i<=m; i++) {
            scanf("%lld", a+i);
            s+=a[i];
        }
        sort(a+1, a+m+1);

        ll ans=0;
        for (int i=1; i<m; i++) {
            s-=a[i];

            if (n*p>d) break;

            ll temp=getMin(d-n*p, s, a[i], n);
            ll b3=max(min((d-n*p)/(s+a[i]), n), 0LL);
            ll b2=max(min(b3, (d-n*p-(d-n*p-b3*s)/a[i]*a[i])/s), 0LL);
            ll b1=max(min(b3, temp+(d-n*p-s*temp)%a[i]/s), 0LL);
            ans = max(ans, max(calc(i, b1), max(calc(i, b2), calc(i, b3))));

            p+=a[i];
        }
        if ((p+a[m])*n<=d) ans=max(ans, n*m+n*k);
        printf("%lld\n", ans);
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}
